package com.gyg.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyg.dao.CloudSpaceMapper;
import com.gyg.domain.*;
import com.gyg.returns.RespCode;
import com.gyg.returns.RespEntity;
import com.gyg.service.*;
import com.gyg.utils.FileUtil;
import com.gyg.vo.result.SpaceResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 云盘空间表 服务实现类
 * </p>
 *
 * @author gyg
 * @since 2021-04-14
 */
@Service
@Slf4j
public class CloudSpaceServiceImpl extends ServiceImpl<CloudSpaceMapper, CloudSpace> implements CloudSpaceService {

    @Autowired
    private CloudPictureService cloudPictureService;

    @Autowired
    private CloudDocumentService cloudDocumentService;

    @Autowired
    private CloudAudioService cloudAudioService;

    @Autowired
    private CloudVideoService cloudVideoService;

    @Override
    public RespEntity<SpaceResult> getCloudSpaceInfo(Integer userId) {

        //轮询图片容量
        Long pictureUsed = cloudPictureService.getPictureByte(userId);
        log.info("用户：{}，图片总容量为：{}", userId, pictureUsed);

        //轮询文档容量
        Long documentUsed = cloudDocumentService.getDocumentByte(userId);
        log.info("用户：{}，文档总容量为：{}", userId, documentUsed);

        //轮询音频容量
        Long audioUsed = cloudAudioService.getAudioByte(userId);
        log.info("用户：{}，音频总容量为：{}", userId, audioUsed);

        //轮询视频容量
        Long videoUsed = cloudVideoService.getVideoByte(userId);
        log.info("用户：{}，视频总容量为：{}", userId, videoUsed);

        //图片、文档、音频、视频 已占用容量总和
        BigDecimal usedTotal = new BigDecimal(pictureUsed).add(new BigDecimal(documentUsed))
                .add(new BigDecimal(audioUsed)).add(new BigDecimal(videoUsed));

        //用户总容量
        long total = baseMapper.getSpaceByte(userId);
        //已用容量占比 四舍五入 保留两位有效数字 75.23
        BigDecimal percent = usedTotal.multiply(new BigDecimal(100)).divide(new BigDecimal(total), 2, BigDecimal.ROUND_HALF_UP);
        //剩余容量
        BigDecimal subtract = new BigDecimal(total).subtract(usedTotal);
        String surplusSize = FileUtil.getSize(subtract.longValue());

        //已用容量
        String usedSize = FileUtil.getSize(usedTotal.longValue());

        //数据组合
        SpaceResult spaceResult = SpaceResult.builder()
                .totalSize(FileUtil.getSize(total))
                .totalByte(total)
                .usedSize(usedSize)
                .usedByte(usedTotal.longValue())
                .percent(percent + "")
                .surplusSize(surplusSize)
                .build();

        //更新用户容量信息
        CloudSpace space = CloudSpace.builder()
                .id(userId)
                .spaceUsedSize(usedSize)
                .spaceUsedByte(usedTotal + "")
                .build();
        baseMapper.updateById(space);
        log.info("更新用户：{}，已用容量成功，已用容量为：{}", userId, usedSize);

        return new RespEntity<>(RespCode.SUCCESS, spaceResult);
    }

    @Override
    public Integer checkCloudSpace(Long usedSize, Integer userId) {
        QueryWrapper<CloudSpace> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(CloudSpace::getDeleted, 0)
                .eq(CloudSpace::getUserId, userId);
        CloudSpace cloudSpace = baseMapper.selectOne(queryWrapper);
        String spaceUsedByte = cloudSpace.getSpaceUsedByte();
        BigDecimal usedTotal = new BigDecimal(usedSize).add(new BigDecimal(spaceUsedByte));
        String spaceTotalByte = cloudSpace.getSpaceTotalByte();
        if (usedTotal.compareTo(new BigDecimal(spaceTotalByte)) > 0) {
            log.info("用户：{}，云盘空间不足，上传失败！", userId);
            return 1;
        }
        return 0;
    }
}
